package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.kejiguan.ui.R;

/**
 * Created by YYY on 2016/5/1.
 */
public class ModifyUserPassDialog extends Dialog implements View.OnClickListener{


    private View mDetailView;
    private EditText passwordInput1,passwordInput2;
    private View okBtn,closeBtn;

    private String pass1;
    private String pass2;

    private OnNewPassListener onNewPassListener;

    public ModifyUserPassDialog(Context context) {
        super(context);
        init(context);
    }

    public ModifyUserPassDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected ModifyUserPassDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context)
    {
        mDetailView = View.inflate(context, R.layout.dialog_modify_user_password_layout, null);

        passwordInput1 = (EditText) mDetailView.findViewById(R.id.new_pass_input1);
        passwordInput2 = (EditText) mDetailView.findViewById(R.id.new_pass_input2);
        okBtn=mDetailView.findViewById(R.id.ok_btn);
        closeBtn=mDetailView.findViewById(R.id.close_btn);
        okBtn.setOnClickListener(this);
        closeBtn.setOnClickListener(this);

        setContentView(mDetailView);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {

            case R.id.close_btn:
                dismiss();
                break;
            case R.id.ok_btn:
                pass1=passwordInput1.getText().toString().trim();
                pass2=passwordInput2.getText().toString().trim();
                if (pass1.equals(pass2))
                {
                    if (!TextUtils.isEmpty(pass1))
                    {
                        if (null!=onNewPassListener)
                        {
                            onNewPassListener.newPass(pass1);
                        }
                        dismiss();
                    }else
                    {
                        Toast.makeText(getContext(),"密码不能为空！",Toast.LENGTH_SHORT).show();
                    }
                }else
                {
                    Toast.makeText(getContext(),"前后两次输入的密码不一致！",Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }

    public ModifyUserPassDialog withOnNewPassListener(OnNewPassListener onNewPassListener)
    {
        this.onNewPassListener=onNewPassListener;
        return  this;
    }


    public interface OnNewPassListener
    {
        public void newPass(String pass);
    }
}
